package com.wanli.storemobile.mine;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.wanli.storemobile.R;
import com.wanli.storemobile.base.BaseFragment;
import com.wanli.storemobile.base.DataCallBack;
import com.wanli.storemobile.bean.BaseResponseBean;
import com.wanli.storemobile.bean.StoreAdminBean;
import com.wanli.storemobile.bean.UserInfoBean;
import com.wanli.storemobile.event.AddressEvent;
import com.wanli.storemobile.event.SwitchStoreEvent;
import com.wanli.storemobile.homepage.model.HomePageModelImpl;
import com.wanli.storemobile.login.LoginActivity;
import com.wanli.storemobile.mine.model.MineModelImpl;
import com.wanli.storemobile.utils.CallPhoneUtil;
import com.wanli.storemobile.utils.SPManager;
import com.wanli.storemobile.utils.VersionUtil;
import com.wanli.storemobile.widget.CommonNoTitleDialog;
import com.wanli.storemobile.widget.ViewLoading;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;

/**
 * 我的
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.tv_merchant_name)
    TextView tvMerchantName;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.ll_weixin_renzheng)
    LinearLayout llWeixinRenzheng;
    @BindView(R.id.ll_address_updata)
    LinearLayout llAddressUpdata;
    @BindView(R.id.ll_message_center)
    LinearLayout llMessageCenter;
    @BindView(R.id.tv_telphone)
    TextView tvTelphone;
    @BindView(R.id.ll_kefu_mobile)
    LinearLayout llKefuMobile;
    @BindView(R.id.ll_ti_xian)
    LinearLayout llTiXian;
    @BindView(R.id.ll_out_login)
    LinearLayout llOutLogin;
    @BindView(R.id.switchview)
    SwitchView switchview;
    @BindView(R.id.ll_bg_launch)
    LinearLayout llBgLaunch;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.ll_about_us)
    LinearLayout llAboutUs;
    @BindView(R.id.ll_update_pwd)
    LinearLayout llUpdatePwd;
    private CommonNoTitleDialog.Builder dialogBuild;
    private UserInfoBean userInfoBean;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        ButterKnife.bind(this, view);
        registerEventBus();
        initView();
        return view;
    }

    @Subscribe
    public void AddressEvent(AddressEvent event) {
        requestUserInfo();
    }

    @Subscribe
    public void onSwitchStoreEvent(SwitchStoreEvent event) {
        requestUserInfo();
    }

    public void initView() {
        tvVersion.setText("V" + VersionUtil.packageName(mActivity));
        if ("true".equals(SPManager.getInstance().getSpeechSwitch())) {
            switchview.setOpened(true);
        } else {
            switchview.setOpened(false);
        }
        switchview.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
            @Override
            public void toggleToOn(SwitchView view) {
                switchview.setOpened(true);
                SPManager.getInstance().saveSpeechSwitch("true");
            }

            @Override
            public void toggleToOff(SwitchView view) {
                switchview.setOpened(false);
                SPManager.getInstance().saveSpeechSwitch("false");
            }
        });
        requestUserInfo();
    }

    public void requestUserInfo() {
        new MineModelImpl().requestMine(new DataCallBack<UserInfoBean>() {
            @Override
            public void onSuccessful(UserInfoBean bean) {
                userInfoBean = bean;
                tvMerchantName.setText(bean.getData().getName());
                tvMobile.setText(SPManager.getInstance().getUserMobile());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick({R.id.ll_bg_launch, R.id.ll_weixin_renzheng, R.id.ll_address_updata, R.id.ll_message_center, R.id.ll_kefu_mobile, R.id.ll_ti_xian, R.id.ll_about_us, R.id.ll_update_pwd, R.id.ll_out_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_bg_launch:
                skipToActivity(LaunchManageActivity.class);
                break;
            case R.id.ll_weixin_renzheng:
                skipToActivity(WeiXinAuthActivity.class);
                break;
            case R.id.ll_address_updata:
                if (userInfoBean == null) {
                    return;
                }
                Intent intent = new Intent(mActivity, ChooseLocationActivity.class);
                intent.putExtra("store_no", userInfoBean.getData().getStore_no());
                intent.putExtra("lat", userInfoBean.getData().getLat() == null ? "" : userInfoBean.getData().getLat());
                intent.putExtra("lng", userInfoBean.getData().getLng() == null ? "" : userInfoBean.getData().getLng());
                mActivity.startActivity(intent);
                break;
            case R.id.ll_message_center:
                skipToActivity(MessageCenterActivity.class);
                break;
            case R.id.ll_kefu_mobile:
                skipToActivity(CustomerServiceWechatActivity.class);
                break;
            case R.id.ll_ti_xian:
                skipToActivity(WithdrawActivity.class);
                break;
            case R.id.ll_about_us:
                skipToActivity(AboutUsActivity.class);
                break;
            case R.id.ll_update_pwd:
                skipToActivity(SettingActivity.class);
                break;
            case R.id.ll_out_login:
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否退出登录?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    outLogin();
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
                break;
        }
    }

    /**
     * 退出登录
     */
    public void outLogin() {
        ViewLoading.showProgress(mActivity, "退出中......");
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        new HomePageModelImpl().requestUnBindDeviceId(pushService.getDeviceId(), new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {

            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });

        SPManager.getInstance().logOffRemove();
        Intent intent = new Intent(mActivity, LoginActivity.class);
        startActivity(intent);
        mActivity.finish();
    }
}
